﻿myApp.controller('RPGTCalculatorController', function ($scope, $http, ngTableParams, $filter, $templateCache) {
    $scope.$emit('newPageLoaded', {
        'title': 'RPGT 2014 Calculator | ShiJie Property',
        'description': 'RPGT 2014 Calculator. Estimate Your RPGT Today Using This Simple To Use RPGT Calculator. Go Now!',
        'keywords': 'rpgt 2014, rpgt 2014 calculator, rpgt calculator'
    });
    //$scope.$emit('newPageLoaded', { 'title': 'Penang Realty 世界地產 RPGT Calculator', 'description': 'Free online RPGT calculator. Try it now.' });



    $scope.totalExpenses = function () {

        $scope.totalExpensesFee = ($scope.other ? $scope.other : 0) + ($scope.repairFee ? $scope.repairFee : 0) + ($scope.administrationFee ? $scope.administrationFee : 0)
            + ($scope.agentCommission ? $scope.agentCommission : 0) + ($scope.legalFee ? $scope.legalFee : 0);

    }

    $scope.calculate = function () {
        validation();
        if ($scope.warning.length > 0)
            return;
        else
            $scope.warning = null
        rpgtRate();
        $scope.exemptionWaiver1 = ($scope.sellingPrice - $scope.propertyPrice - ($scope.totalExpensesFee ? $scope.totalExpensesFee : 0)) * 0.1;
        $scope.rpgt1 = $scope.rpgtRate1 / 100 * ($scope.sellingPrice - $scope.propertyPrice - ($scope.totalExpensesFee ? $scope.totalExpensesFee : 0) - $scope.exemptionWaiver1);
        $scope.rpgt2 = $scope.rpgtRate2 / 100 * ($scope.sellingPrice - $scope.propertyPrice - ($scope.totalExpensesFee ? $scope.totalExpensesFee : 0));
        $scope.rpgt3 = $scope.rpgtRate3 / 100 * ($scope.sellingPrice - $scope.propertyPrice - ($scope.totalExpensesFee ? $scope.totalExpensesFee : 0));
        // Net gain
        $scope.netGain1 = $scope.sellingPrice - $scope.propertyPrice - ($scope.totalExpensesFee ? $scope.totalExpensesFee : 0) - $scope.rpgt1;
        $scope.netGain2 = $scope.sellingPrice - $scope.propertyPrice - ($scope.totalExpensesFee ? $scope.totalExpensesFee : 0) - $scope.rpgt2;
        $scope.netGain3 = $scope.sellingPrice - $scope.propertyPrice - ($scope.totalExpensesFee ? $scope.totalExpensesFee : 0) - $scope.rpgt3;
        $scope.calFinish = true;
    }

    function rpgtRate() {
        switch ($scope.holdingYear) {
            case 1:
                $scope.rpgtRate1 = 30;
                $scope.rpgtRate2 = 30;
                $scope.rpgtRate3 = 30;
                break;
            case 2:
                $scope.rpgtRate1 = 30;
                $scope.rpgtRate2 = 30;
                $scope.rpgtRate3 = 30;
                break;
            case 3:
                $scope.rpgtRate1 = 30;
                $scope.rpgtRate2 = 30;
                $scope.rpgtRate3 = 30;
                break;
            case 4:
                $scope.rpgtRate1 = 20;
                $scope.rpgtRate2 = 20;
                $scope.rpgtRate3 = 30;
                break;
            case 5:
                $scope.rpgtRate1 = 15;
                $scope.rpgtRate2 = 15;
                $scope.rpgtRate3 = 30;
                break;

            default:
                $scope.rpgtRate1 = 0;
                $scope.rpgtRate2 = 5;
                $scope.rpgtRate3 = 5;
        }
    }
    $scope.close = function () {

        $scope.warning = null;
        $scope.error = null;
        $scope.info = null;
    }

    function validation() {
        $scope.warning = [];
        if (!$scope.sellingPrice)
            $scope.warning.push({ msg: "Selling price must not be empty!" })
        if (!$scope.propertyPrice)
            $scope.warning.push({ msg: "Purchase price must not be empty!" })
        if (!$scope.holdingYear)
            $scope.warning.push({ msg: "Holding year must not be empty!" })

    }


});